﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;

namespace AdslMonitorViewer
{
    public partial class ChartViewer : Form
    {
        public ChartViewer()
        {
            InitializeComponent();
        }

        private void ChartViewer_Load(object sender, EventArgs e)
        {
            RefreshChart();
        }

        private void RefreshChart()
        {
            string[] file = File.ReadAllLines(@"c:\temp\modem.log");
            DataTable dt = new DataTable("log");
            dt.Columns.Add(new DataColumn("DateTime"));
            dt.Columns.Add(new DataColumn("dB"));
            for (int i = file.Length - 1; i >= 0; i--)
            {
                dt.Rows.Add(file[i].Split('\t'));
            }
            //foreach (string item in file)
            //{
            //    dt.Rows.Add(item.Split('\t'));
            //}

            chartSnr.ChartAreas.Clear();
            chartSnr.Series.Clear();
            chartSnr.ChartAreas.Add("Area1");
            chartSnr.Series.Add("series0");
            chartSnr.Series[0].ChartType = SeriesChartType.Line;

            //chart1.Series[0].IsXValueIndexed = true;
            chartSnr.Series[0].YValueMembers = "db";
            chartSnr.Series[0].XValueMember = "DateTime";
            chartSnr.Series[0].ChartArea = "Area1"; ;
            // bind
            chartSnr.DataSource = dt;
            chartSnr.DataBind();

            chartSnr.Legends.Clear();

            var chartArea = chartSnr.ChartAreas[chartSnr.Series[0].ChartArea];

            // set view range to [0,max]
            chartArea.AxisX.Minimum = 0;
            chartArea.AxisX.Maximum = dt.Rows.Count;

            // enable autoscroll
            chartArea.CursorX.AutoScroll = true;
            int blocksize = 480;

            // let's zoom to [0,blockSize] (e.g. [0,100])
            chartArea.AxisX.ScaleView.Zoomable = true;
            chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
            int position = 0;
            int size = blocksize;
            chartArea.AxisX.ScaleView.Zoom(position, size);

            // disable zoom-reset button (only scrollbar's arrows are available)
            chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;

            // set scrollbar small change to blockSize (e.g. 100)
            chartArea.AxisX.ScaleView.SmallScrollSize = blocksize;
        }

        private void ChartViewer_SizeChanged(object sender, EventArgs e)
        {
            chartSnr.Width = this.Width - 20;
            chartSnr.Height = this.Height - 40;
        }

        private void chartSnr_DoubleClick(object sender, EventArgs e)
        {
            this.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            RefreshChart();
            this.Cursor = DefaultCursor;
            this.Enabled = true;
        }
    }
}
